<template>
  <basic-container>
    <avue-crud
      ref="crud"
      v-model="form"
      :data="data"
      :option="option"
      :page.sync="page"
      :before-open="beforeOpen"
      @on-load="rowLoad"
      @search-change="searchChange"
      @row-update="rowUpdate"
      @row-del="rowDel"
      @row-save="rowSave"
      @size-change="sizeChange"
      @current-change="currentChange"
    >
      <template slot-scope="{ type, size, row }" slot="menu">
        <el-button
          icon="el-icon-user"
          :size="size"
          :type="type"
          @click="signUpUsers(row)"
          >报名人员</el-button
        >
      </template>
      <template slot-scope="{ type, disabled }" slot="coverForm">
        <el-upload
          :action="baseUrl + 'api/oss/upload'"
          list-type="picture-card"
          :limit="1"
          :file-list="fileList"
          :on-success="onSuccess"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
        >
          <!-- <img v-if="form.cover" :src="form.cover" class="avatar" /> -->
          <!-- v-else -->
          <i class="el-icon-plus"></i>
        </el-upload>
      </template>
    </avue-crud>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
    <users ref="users" />
  </basic-container>
</template>

<script>
import avue from "@/mixins/avue.js";
import { baseUrl } from "@/config/env";
import { SE } from "@/common/constants.js";
import {
  activity_page,
  activity_edit,
  activity_add,
  activity_delete,
} from "@/api/alumni";

import users from "./users.vue";

export default {
  mixins: [avue],
  components: {
    users,
  },
  data() {
    return {
      baseUrl,
      dialogImageUrl: "",
      dialogVisible: false,
      drawer: false,
      data: [],
      fileList: [],
      option: {
        column: [
          {
            label: "封面",
            prop: "cover",
            // formslot: true,
            type: "upload",
            fileType: "img",
            props: {
              label: "fileId",
              value: "previewUrl",
            },
            limit: 1,
            propsHttp: {
              // home: baseUrl,
              // res: "data",
              name: "fileName",
              url: "previewUrl",
            },
            span: 24,
            listType: "picture-card",
            action: "app/uploadFile",
          },
          {
            label: "标题",
            prop: "title",
            align: "left",
            search: true,
          },
          // {
          //   label: "内容",
          //   prop: "content",
          //   align: "left",
          // },
          {
            label: "地址",
            prop: "address",
          },
          {
            label: "开始时间",
            prop: "startTime",
            type: "datetime",
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
          },
          {
            label: "结束时间",
            prop: "endTime",
            type: "datetime",
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
          },
          {
            label: "状态",
            prop: "status",
            type: "radio",
            // display: false,
            dicData: [
              {
                label: "未开始",
                value: 0,
              },
              {
                label: "进行中",
                value: 1,
              },
              {
                label: "已结束",
                value: -1,
              },
            ],
            value: 0,
          },
          {
            label: "内容",
            prop: "content",
            span: 24,
            hide: true,
            component: "avueUeditor",
            // action: "api/oss/upload",
            action: "app/uploadFile",
            propsHttp: {
              // home: baseUrl,
              // res: "data",
              name: "fileName",
              url: "previewUrl",
            },
          },
        ],
      },
      tableData: [],
    };
  },
  methods: {
    onSuccess(res, file, fileList) {
      console.log(res, file, fileList);
      this.form.cover = res.result.previewUrl;
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      console.log(file);
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    signUpUsers(row) {
      this.$refs.users.handleOpen(row);
    },
    beforeOpen(done, type) {
      if (["edit"].includes(type)) {
        if (this.form.cover) {
          this.fileList = [{ url: this.form.cover }];
        }
      }
      done();
    },
    rowLoad() {
      activity_page(this.params).then((res) => {
        let result = this.$s.res(res);
        this.data = result.records;
        this.page.total = result.totalPage;
      });
    },
    rowSave(row, loading, done) {
      activity_add(row)
        .then((res) => {
          this.$s.suc(res);
        })
        .then(() => {
          this.rowLoad();
          done();
          loading();
        })
        .catch(() => {
          loading();
        });
    },
    rowUpdate(row, index, done, loading) {
      activity_edit(row)
        .then((res) => {
          this.$s.suc(res);
        })
        .then(() => {
          this.rowLoad();
          done();
          loading();
        })
        .catch(() => {
          loading();
        });
    },
    rowDel(row, index, done) {
      this.$confirm("确定数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          activity_delete({ id: row.id }).then((res) => {
            this.$s.suc(res);
          });
        })
        .then(() => {
          this.rowLoad();
          done();
        });
    },
  },
  mounted() {},
};
</script>

<style lang="less" scoped>
.demo-drawer__content {
  width: 100%;
  padding: 20px;
}
</style>
